package com.guigu.qingcheng.servicegoods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.qingcheng.pojo.goods.TbPara;
import com.guigu.qingcheng.pojo.goods.TbTemplate;
import com.guigu.qingcheng.servicegoods.mapper.TbParaMapper;
import com.guigu.qingcheng.servicegoods.service.TbParaService;
import com.guigu.qingcheng.servicegoods.service.TbTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class TbParaServiceImpl extends ServiceImpl<TbParaMapper, TbPara> implements TbParaService {

    @Resource
    private TbParaMapper mapper;

    @Autowired
    private TbParaService tbParaService;

    @Autowired
    private TbTemplateService templateService;

    //根据条件查询封装查询对象
    public QueryWrapper<TbPara> queryWrapper(Map<String, Object> searchMap){
        QueryWrapper<TbPara> wapper = new QueryWrapper<TbPara>();
        if(searchMap != null){
            if(searchMap.get("id") != null && !"".equals(searchMap.get("id")))
                wapper.eq("id", searchMap.get("id"));
            if(searchMap.get("name") != null && !"".equals(searchMap.get("name")))
                wapper.like("name", searchMap.get("name"));
            if(searchMap.get("options") != null && !"".equals(searchMap.get("options")))
                wapper.like("options", searchMap.get("options"));
            if(searchMap.get("seq") != null && !"".equals(searchMap.get("seq")))
                wapper.eq("seq", searchMap.get("seq"));
            if(searchMap.get("templateId") != null && !"".equals(searchMap.get("templateId")))
                wapper.eq("template_id", searchMap.get("templateId"));
        }
        return wapper;
    }

    @Override
    public Page<TbPara> findPage(Map<String, Object> searchMap, int page, int size) {
        Page<TbPara> pageTbPara = new Page<TbPara>(page, size);
        QueryWrapper<TbPara> query = queryWrapper(searchMap);
        IPage<TbPara> list = this.tbParaService.page(pageTbPara, query);
        return pageTbPara;
    }

    @Override
    public List<TbPara> findList(Map<String, Object> searchMap) {
        QueryWrapper<TbPara> wrapper = queryWrapper(searchMap);
        List<TbPara> list = this.tbParaService.list(wrapper);
        return list;
    }

    @Transactional
    @Override
    public Boolean addPara(TbPara para) {
        //1.先添加一条数据到TbPara

        Boolean fag = this.tbParaService.save(para);
        //2. 同时修改下对应模板的参数总数：+1
        Integer templateId = para.getTemplateId();
        TbTemplate template = this.templateService.getById(templateId);
        template.setParaNum(template.getParaNum() + 1);
        Boolean flag = this.templateService.updateById(template);
        if (fag == flag ==true)
            return true;
        return false;
    }

    @Transactional
    @Override
    public Boolean deletePara(Integer id) {
        // 同时修改下对应模板的参数总数：-1
        TbPara para = this.tbParaService.getById(id);
        TbTemplate tbTemplate = this.templateService.getById(para.getTemplateId());
        tbTemplate.setParaNum(tbTemplate.getParaNum() - 1);
        Boolean b = this.templateService.updateById(tbTemplate);
        Boolean aBoolean = this.tbParaService.removeById(id);
        if (b == aBoolean ==true)
            return true;
        return  false;

    }
}
